package com.wing.area.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wing.area.model.entity.Area;
import com.wing.area.service.AreaService;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.web.db.config.util.DynamicDataSource;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@Api(tags = "系统区域模块API")
public class AreaController {

	@Autowired
	private AreaService areaService;

	/**
	 * 省份列表
	 */
	@PostMapping("/area/getProvinceList")
	@ResponseBody
	JsonResult getProvinceList() {
		try {

			return JsonResult.success("成功!", areaService.getProvinceList());
		} catch (BusinessException e) {
			return JsonResult.fail(e.getCode(), e.getMessage());
		} catch (Exception e) {
			return JsonResult.fail(e.getMessage());
		}
	}

	/**
	 * 市区列表
	 */
	@PostMapping("/area/getCityList")
	@ResponseBody
	JsonResult getCityList(@RequestParam Integer parentId) {
		try {

			return JsonResult.success("成功!", areaService.getCityList(parentId));
		} catch (BusinessException e) {
			return JsonResult.fail(e.getCode(), e.getMessage());
		} catch (Exception e) {
			return JsonResult.fail(e.getMessage());
		}
	}

	/**
	 * 区域列表
	 */
	@PostMapping("/area/getAreaList")
	@ResponseBody
	JsonResult getAreaList(@RequestParam Integer parentId) {
		try {

			return JsonResult.success("成功!", areaService.getAreaList(parentId));
		} catch (BusinessException e) {
			return JsonResult.fail(e.getCode(), e.getMessage());
		} catch (Exception e) {
			return JsonResult.fail(e.getMessage());
		}
	}

	public Area getArea(Integer id) {

		QueryWrapper<Area> areaQuery = new QueryWrapper<Area>();
		areaQuery.eq("id", id);
		Area area = areaService.getById(id);
		return area;
	}

}
